home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / info-service / wais / ir-book-sources / bool / bvdriver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  9.3 KB  |  324 lines

  1. /***************************  driver.c  **************************************
  2.  
  3.   Purpose:    Test driver program for boolean operations code.
  4.  
  5.   Provenance:    Written and tested by S. Wartik, April 1991.
  6.  
  7.   Notes:    This module contains a main program and accompanying
  8.           subroutines that, when compiled, exercise every function
  9.         of the boolean operations module.  The testing is purely
  10.         in terms of the external interface that the boolean
  11.         operations module presents.  That is, the effects of
  12.         constructor functions are examined through the projector
  13.         functions.  This makes the code (almost) implementation-
  14.         independent.  For this reason, it may be used (with slight
  15.         modifications, controlled through #ifdef's) to test both
  16.         the bit-vector and the hashing implementations.
  17.         
  18.           Usage for the bit-vector version is:
  19.  
  20.             driver lower-bound upper-bound
  21.  
  22.         where the bounds specify the legal domain of the set.  In
  23.         the current implementation, abs(upper-bound - lower-bound)
  24.         must be less than 256.
  25.             
  26. **/
  27. #include    <stdio.h>
  28.  
  29. #    include    "bv.h"
  30.  
  31. #define N_SETS        3
  32.  
  33. #define MIN_ELEMENTS    5        /* Make the tests "interesting". */
  34.                     /* Require at least 5 elements.     */
  35. void    Exit_If_Error_On();
  36. void    Exit_Indicating_Failed_Op();
  37.  
  38.  
  39. void    Test_Set_Operators();        /* The main routine is implemented  */
  40. void    Test_Union_Operators();        /* as a series of calls to several  */
  41. void    Test_Intersection_Operators();    /* procedures, each of which tests  */
  42. void    Test_Subtraction_Operators();    /* a particular aspect of the        */
  43. void    Test_Copy();            /* module.                */
  44. void    Test_Iterate();
  45.  
  46. boolean    Iterator();
  47.  
  48.  
  49. main(argc, argv)
  50.     int    argc;
  51.     char    *argv[];
  52. {
  53.     set    s[N_SETS];
  54.     int    lower, upper;
  55.     int    i;
  56.  
  57.     if ( argc != 3 ) {
  58.         printf("Usage: %s lower-bound upper-bound\n", argv[0]);
  59.         exit(1);
  60.     }
  61.  
  62.     lower = atoi(argv[1]);
  63.     upper = atoi(argv[2]);
  64.     if ( upper < lower+MIN_ELEMENTS ) {
  65.         printf("Please specify a set that can contain at least %d elements.\n",
  66.                MIN_ELEMENTS);
  67.         exit(1);
  68.     }
  69.     else if ( upper - lower > MAX_ELEMENTS-1 ) {
  70.         printf("This implementation can't accommodate upper-lower>%d.\n",
  71.                MAX_ELEMENTS-1);
  72.         exit(1);
  73.     }
  74.  
  75.     for ( i = 0; i < N_SETS; i++ ) {    /* Test creation. */
  76.         Create(lower, upper, &s[i]);
  77.         Exit_If_Error_On("Create");
  78.     }
  79.     fputs("Creation works.\n", stdout);
  80.  
  81.     for ( i = 0; i < 2; i++ ) {        /* Test clearing sets. */
  82.         Clear(&s[i]);
  83.         Exit_If_Error_On("Clear");
  84.         if ( Empty(&s[i]) )
  85.             Exit_If_Error_On("Empty");
  86.         else    Exit_Indicating_Failed_Op("Empty");
  87.     }
  88.     fputs("Clearing works.\n", stdout);
  89.  
  90.     Test_Set_Operators(s, lower, upper);
  91.     fputs("Basic operators works.\n", stdout);
  92.     Test_Union_Operators(s, lower, upper);
  93.     fputs("Union works.\n", stdout);
  94.     Test_Intersection_Operators(s, lower, upper);
  95.     fputs("Intersection works.\n", stdout);
  96.     Test_Subtraction_Operators(s, lower, upper);
  97.     fputs("Subtraction works.\n", stdout);
  98.     Test_Copy(s, lower, upper);
  99.     fputs("Copying works.\n", stdout);
  100.     Test_Iterate(s, lower, upper);
  101.     fputs("Iterating works.\n", stdout);
  102.  
  103.     fputs("\nThe implementation passes.\n", stdout);
  104.     exit(0);
  105. }
  106.  
  107. void Test_Set_Operators(s, lower, upper)
  108.     set    s[];
  109.     int    lower, upper;
  110. {
  111.     int    i;
  112.     
  113.     for ( i = lower; i <= upper; i++ ) {    /* Test insertion of all    */
  114.         Insert(&s[0], i);        /* valid elements for a     */
  115.         Exit_If_Error_On("Insert");    /* set that doesn't already */
  116.         if ( Member(&s[0], i) )        /* contain them.        */
  117.             Exit_If_Error_On("Member");
  118.         else    Exit_Indicating_Failed_Op("Member");
  119.     }
  120.  
  121.     for ( i = lower; i <= upper; i++ ) {    /* Test insertion of all */
  122.         Insert(&s[0], i);        /* valid elements for a  */
  123.         Exit_If_Error_On("Insert");    /* set that already has  */
  124.         if ( Member(&s[0], i) )        /* them.         */
  125.             Exit_If_Error_On("Member");
  126.         else    Exit_Indicating_Failed_Op("Member");
  127.     }
  128.  
  129.     for ( i = lower; i <= upper; i++ ) {    /* Test deletion of all  */
  130.         Delete(&s[0], i);        /* valid elements when   */
  131.         Exit_If_Error_On("Delete");    /* the set already has     */
  132.         if ( ! Member(&s[0], i) )    /* them.         */
  133.             Exit_If_Error_On("Delete");
  134.         else    Exit_Indicating_Failed_Op("Member");
  135.     }
  136.  
  137.     for ( i = lower; i <= upper; i++ ) {    /* Test deletion of all  */
  138.         Delete(&s[0], i);        /* valid elements when   */
  139.         Exit_If_Error_On("Delete");    /* the set doesn't have     */
  140.         if ( ! Member(&s[0], i) )    /* them.         */
  141.             Exit_If_Error_On("Delete");
  142.         else    Exit_Indicating_Failed_Op("Member");
  143.     }
  144. }
  145.  
  146. void Test_Union_Operators(s, lower, upper)
  147.     set    s[];
  148.     int    lower, upper;
  149. {
  150.     int    i;
  151.  
  152.     Insert(&s[0], lower);            /* Test union of a set    */
  153.     Exit_If_Error_On("Insert");        /* with an empty set.      */
  154.     Unite(&s[0], &s[1], &s[2]);        /*   s2 <- {lower}U{}      */
  155.     Exit_If_Error_On("Unite");
  156.     if ( Member(&s[2], lower) )
  157.         Exit_If_Error_On("Member");
  158.     else    Exit_Indicating_Failed_Op("Member|Unite");
  159.     for ( i = lower+1; i <= upper; i++ )
  160.         if ( Member(&s[2], i) )
  161.             Exit_Indicating_Failed_Op("Member|Unite");
  162.         else    Exit_If_Error_On("Member");
  163.  
  164.     Insert(&s[1], lower+1);            /* Test union of two non-    */
  165.     Exit_If_Error_On("Insert");        /* empty, non-intersecting   */
  166.     Unite(&s[0], &s[1], &s[2]);        /* sets.             */
  167.     Exit_If_Error_On("Unite");        /*   s2 <- {lower}U{lower+1} */
  168.     if ( Member(&s[2], lower) && Member(&s[2], lower+1) )
  169.         Exit_If_Error_On("Member");
  170.     else    Exit_Indicating_Failed_Op("Member|Unite");
  171.     for ( i = lower+2; i <= upper; i++ )
  172.         if ( Member(&s[2], i) )
  173.             Exit_Indicating_Failed_Op("Member|Unite");
  174.         else    Exit_If_Error_On("Member");
  175.  
  176.                     /* Test union of two non-empty,   */
  177.     Insert(&s[0], lower+1);        /* intersecting sets.          */
  178.     Exit_If_Error_On("Insert");    /*   s2 <- {lower,lower+1} U      */
  179.     Unite(&s[0], &s[1], &s[2]);    /*        {lower+1}      */
  180.     Exit_If_Error_On("Unite");
  181.     if ( Member(&s[2], lower) && Member(&s[2], lower+1) )
  182.         Exit_If_Error_On("Member");
  183.     else    Exit_Indicating_Failed_Op("Member|Unite");
  184.     for ( i = lower+2; i <= upper; i++ )
  185.         if ( Member(&s[2], i) )
  186.             Exit_Indicating_Failed_Op("Member|Unite");
  187.         else    Exit_If_Error_On("Member");
  188. }
  189.  
  190. void Test_Intersection_Operators(s, lower, upper)
  191.     set    s[];
  192.     int    lower, upper;
  193. {
  194.     int    i;
  195.  
  196.     Clear(&s[0]);
  197.     Clear(&s[1]);
  198.  
  199.     Insert(&s[0], lower);            /* Test intersection of a */
  200.     Exit_If_Error_On("Insert");        /* set with an empty set. */
  201.     Intersect(&s[0], &s[1], &s[2]);        /*   s2 <- {lower}^{}      */
  202.     Exit_If_Error_On("Intersect");
  203.     for ( i = lower; i <= upper; i++ )
  204.         if ( Member(&s[2], i) )
  205.             Exit_Indicating_Failed_Op("Member|Intersect");
  206.         else    Exit_If_Error_On("Member");
  207.  
  208.     Insert(&s[0], lower);            /* Test intersection of two    */
  209.     Exit_If_Error_On("Insert");        /* non-empty, non-intersecting */
  210.     Insert(&s[1], lower+1);            /* sets.                  */
  211.     Exit_If_Error_On("Insert");        /*   s2 <- {lower}^{lower+1}   */
  212.     Intersect(&s[0], &s[1], &s[2]);
  213.     Exit_If_Error_On("Intersect");
  214.     for ( i = lower; i <= upper; i++ )
  215.         if ( Member(&s[2], i) )
  216.             Exit_Indicating_Failed_Op("Member|Intersect");
  217.         else    Exit_If_Error_On("Member");
  218.  
  219.     Insert(&s[0], lower);            /* Test intersection of two non- */
  220.     Exit_If_Error_On("Insert");        /* non-empty, intersecting sets. */
  221.     Insert(&s[0], lower+1);            /*   s2 <- {lower,lower+1}^         */
  222.     Exit_If_Error_On("Insert");        /*          {lower,lower+2}    */
  223.     Clear(&s[1]);
  224.     Insert(&s[1], lower);
  225.     Exit_If_Error_On("Insert");
  226.     Insert(&s[1], lower+2);
  227.     Exit_If_Error_On("Insert");
  228.     Intersect(&s[0], &s[1], &s[2]);
  229.     Exit_If_Error_On("Intersect");
  230.     if ( Member(&s[2], lower) )
  231.         Exit_If_Error_On("Member");
  232.     else    Exit_Indicating_Failed_Op("Member|Intersect");
  233.     for ( i = lower+1; i <= upper; i++ )
  234.         if ( Member(&s[2], i) )
  235.             Exit_Indicating_Failed_Op("Member|Intersect");
  236.         else    Exit_If_Error_On("Member");
  237. }
  238.  
  239. void Test_Subtraction_Operators(s, lower, upper)
  240.     set    s[];
  241.     int    lower, upper;
  242. {
  243.     int    i;
  244.  
  245.     Clear(&s[0]);                /* Subtract one empty set   */
  246.     Clear(&s[1]);                /* from another.        */
  247.     Subtract(&s[0], &s[1], &s[2]);
  248.     Exit_If_Error_On("Subtract");
  249.     for ( i = lower; i <= upper; i++ )
  250.         if ( Member(&s[2], i) )
  251.             Exit_Indicating_Failed_Op("Member|Subtract");
  252.         else    Exit_If_Error_On("Member");
  253. }
  254.  
  255. void Test_Copy(s, lower, upper)
  256.     set    s[];
  257.     int    lower, upper;
  258. {
  259.     int    i;
  260.     
  261.     Clear(&s[0]);                 /* Create a set with every */
  262.     for ( i = lower; i <= upper; i += 3 ) {     /* third possible value,   */
  263.         Insert(&s[0], i);         /* copy it, and make sure  */
  264.         Exit_If_Error_On("Insert");     /* both sets have the same */
  265.     }                     /* members.            */
  266.  
  267.     Copy(&s[0], &s[1]);
  268.  
  269.     for ( i = lower; i <= upper; i++ )
  270.         if ( Member(&s[0], i) != Member(&s[1], i) )
  271.             Exit_Indicating_Failed_Op("Member|Copy");
  272.         else    Exit_If_Error_On("Member");
  273. }
  274.  
  275. int    Count_Of_Elements_Iterated_Over;
  276.  
  277. boolean Iterator(e)
  278.     int    e;
  279. {
  280.     if ( e % 2 != 0 )    /* passed a value that wasn't inserted. */
  281.         Exit_Indicating_Failed_Op("Iterate");
  282.     Count_Of_Elements_Iterated_Over++;
  283.     return TRUE;
  284. }
  285.  
  286. void Test_Iterate(s, lower, upper)
  287.     set    s[];
  288.     int    lower, upper;
  289. {
  290.     int    i;
  291.     int    Count_Of_Elements_Added;
  292.  
  293.     Count_Of_Elements_Added = Count_Of_Elements_Iterated_Over = 0;
  294.  
  295.     Clear(&s[0]);
  296.     for ( i = (lower/2)*2 + 2; i <= upper; i += 2 ) {
  297.         Insert(&s[0], i);        /* Insert all even values */
  298.         Exit_If_Error_On("Insert");    /* (except perhaps the      */
  299.         Count_Of_Elements_Added++;    /* first) into the set.   */
  300.     }
  301.  
  302.     Iterate(&s[0], Iterator);
  303.     Exit_If_Error_On("Iterate");
  304.     if ( Count_Of_Elements_Added != Count_Of_Elements_Iterated_Over )
  305.         Exit_Indicating_Failed_Op("Iterate");
  306. }
  307.  
  308. void Exit_If_Error_On(operation)
  309.     char    operation[];
  310. {
  311.     if ( ! Error_Occurred() )
  312.         return;
  313.     printf("%s operation caused an error.\n", operation);
  314.     exit(1);
  315. }
  316.  
  317.  
  318. void Exit_Indicating_Failed_Op(operation)
  319.     char    operation[];
  320. {
  321.     printf("%s operation failed to function correctly.\n", operation);
  322.     exit(1);
  323. }
  324.